﻿using Common.AspNetCore;
using Common.Infrastructure;
using StatsData.Api.Models.BoardQuery;
using StatsData.Api.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace StatsData.Api.Service
{
    public class BoardQueryService : CommonService
    {
        private readonly BoardQueryRepository boardQueryRepository;

        public BoardQueryService(IServiceProvider provider, BoardQueryRepository boardQueryRepository) : base(provider)
        {
            this.boardQueryRepository = boardQueryRepository;
        }

        /// <summary>
        /// 品质日看板获取CSI数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<QCBoardCSIRecordResultModel>> GetCSIByQcBoardAsync(QCBoardCSIRecordParamModel param)
        {
            LibCheckNullUtils.CheckNull(param, "param");
            LibCheckNullUtils.CheckNull(param.LineCodes, "param.LineCodes");
            LibCheckNullUtils.CheckNull(param.OrgId, "param.OrgId");

            var csiEntity = await this.boardQueryRepository.GetCSIByQcBoardAsync(param.LineCodes, param.BeginTime, param.EndTime, param.OrgId);

            if (csiEntity == null)
            {
                return new List<QCBoardCSIRecordResultModel>();
            }

            var res = new List<QCBoardCSIRecordResultModel>();

            foreach (var entity in csiEntity)
            {
                var m = new QCBoardCSIRecordResultModel()
                {
                    Batch = entity.batch,
                    CurrDate = entity.curr_date,
                    BeginTime = entity.begin_time,
                    EndTime = entity.end_time,
                    CustNo = entity.cust_no,
                    ItemNo = entity.item_no,
                    PdtNo = entity.pdt_no,
                    Result = entity.result,
                    ResultNo = entity.result_no,
                    SoItem = entity.so_item,
                    SoNo = entity.so_no,
                    Type = entity.type,
                };

                res.Add(m);
            }

            return res;
        }
    }
}
